package com.doyens.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.doyens.gmall.item.service.ItemService;
import com.doyens.gmall.model.product.BaseCategoryView;
import com.doyens.gmall.model.product.SkuInfo;
import com.doyens.gmall.model.product.SpuSaleAttr;
import com.doyens.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Override
    public Map<String, Object> getItemBySkuId(Long skuId) {
        //创建一个map容器，用于存储从product工程获取到的数据
        Map<String,Object> result = new HashMap<>();
        //获取skuInfo数据
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        if (skuInfo != null){
            //价格 三级分类
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            result.put("price",skuPrice);
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView",categoryView);
            //销售属性
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            result.put("spuSaleAttrList",spuSaleAttrListCheckBySku);
            //销售属性值
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String valueJson = JSON.toJSONString(skuValueIdsMap);
            result.put("valuesSkuJson",valueJson);
            //添加到map集合中
            result.put("skuInfo",skuInfo);
        }

        return result;
    }
}
